<?php

namespace app\logic;

use app\model\Group as GroupModel;
use app\model\Menu as MenuModel;
use app\model\Sjoin as SjoinModel;
use think\db\Query;

class AuthLogic
{
    /**
     * @var array
     */
    private array $getGroupIdArr = [];

    /**
     * @var GroupModel
     */
    private GroupModel $groupModel;

    /**
     * @var MenuModel
     */
    private MenuModel $menuModel;

    /**
     * @var SjoinModel
     */
    private SjoinModel $sjoinModel;

    public $adminInfo;

    public function __construct()
    {
        $this->groupModel = new GroupModel();
        $this->menuModel = new MenuModel();
        $this->sjoinModel = new SjoinModel();
        $this->adminInfo = request()->ADMIN_INFO;
    }


    /**
     * 查看菜单权限
     *
     * @param $uid
     * @param int $type
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserExt($adminId, $type = 0): string
    {
        $guid = '-1';
        if ($type == 1) {
            return $guid;
        }
        $guid = '[0]';
        $mid = $this->getMenuId($adminId);
        $subQuery = $this->menuModel->field('pid')->where('id', 'a.pid')->buildSql();
        $arss = $this->menuModel
            ->alias('a')
            ->field('id, pid')
            ->field($subQuery . ' as mpid')
            ->whereOr([
                [['status', '=', 1], ['id', 'in', $mid]],
                [['status', '=', 1], ['ispir', '=', 0]]
            ])
            ->order('sort')
            ->select()
            ->toArray();

        foreach ($arss as $ars) {
            $guid .= ',[' . $ars['id'] . ']';
            $bpid = $ars['pid'];
            $bmpid = $ars['mpid'];
            if (!contain($guid, '[' . $bpid . ']')) {
                $guid .= ',[' . $bpid . ']';
            }
            if (!empty($bmpid)) {
                if (!contain($guid, '[' . $bmpid . ']')) {
                    $guid .= ',[' . $bmpid . ']';
                }
            }
        }

        return $guid;
    }

    /**
     * 获取权限菜单id
     *
     * @param $adminId
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMenuId($adminId): string
    {
        $gid = $this->getGroupId($adminId);

        $sids = $this->sjoinModel
            ->field('sid')
            ->whereOr([
                function (Query $query) use ($adminId) {
                    $query->where('type', 'um')->where('mid', $adminId);
                },
                function (Query $query) use ($gid, $adminId) {
                    if ($gid != '0') {
                        $query->where('type', 'gm')->whereIn('mid', $gid);
                    }
                }
            ])
            ->select()
            ->toArray();

        $mids = $this->sjoinModel
            ->field('mid')
            ->whereOr([
                function (Query $query) use ($adminId) {
                    $query->where('type', 'mu')->where('sid', $adminId);
                },
                function (Query $query) use ($gid, $adminId) {
                    if ($gid != '0') {
                        $query->where('type', 'mg')->whereIn('sid', $gid);
                    }
                }
            ])
            ->select()
            ->toArray();

        $ids = '0';
        foreach ($sids as $rs) {
            $ids .= ',' . $rs['sid'] . '';
        }

        foreach ($mids as $rs) {
            $ids .= ',' . $rs['mid'] . '';
        }

        return $ids;
    }

    /**
     * 获取用户所在组Id
     *
     * @param $adminId
     * @param string $fid
     * @return mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGroupId($adminId, string $fid = '')
    {
        if ($fid == '') {
            $fid = 'id';
        }
        $keys = '' . $fid . '' . $adminId . '';
        if (isset($this->getGroupIdArr[$keys])) {
            return $this->getGroupIdArr[$keys];
        }

        $rows = $this->groupModel
            ->field('id')
            ->whereOr([
                function (Query $query) use ($adminId) {
                    $query->whereIn('id', function (Query $query) use ($adminId) {
                        $query->name('sjoin')
                            ->field('sid')
                            ->where('type', 'ug')
                            ->where('mid', $adminId);
                    });
                },
                function (Query $query) use ($adminId) {
                    $query->whereIn('id', function (Query $query) use ($adminId) {
                        $query->name('sjoin')
                            ->field('mid')
                            ->where('type', 'gu')
                            ->where('sid', $adminId);
                    });
                }
            ])
            ->select()
            ->toArray();

        $ids = '0';
        foreach ($rows as $rs) {
            $ids .= ',' . $rs[$fid] . '';
        }

        $this->getGroupIdArr[$keys] = $ids;
        return $ids;
    }
}